package cn.lg.soar.boot.fc.service.impl;

import cn.lg.soar.boot.fc.entity.Furniture;
import cn.lg.soar.boot.fc.service.FurnitureService;
import cn.lg.soar.common.data.msg.I18n;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.common.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.lg.soar.database.base.service.impl.BaseServiceImpl;
import cn.lg.soar.boot.fc.mapper.RoomFurnitureMapper;
import cn.lg.soar.boot.fc.entity.RoomFurniture;
import cn.lg.soar.boot.fc.service.RoomFurnitureService;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 房间配套分配表
 * @author BoBo
 * @since 2025-01-20
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Service
public class RoomFurnitureServiceImpl extends BaseServiceImpl<RoomFurnitureMapper, RoomFurniture> implements RoomFurnitureService {

    @Autowired
    private FurnitureService furnitureService;

    @Autowired RoomFurnitureService myService;

    @Override
    public HttpResult<Serializable> addRoomFurniture(RoomFurniture roomFurniture) {
        //房间配套数量
        //Integer romfutotla = roomFurniture.getRomfutotla();
        //roomFurniture.getFurnitureid();// 配套表id;
        //查询剩余配套数量
        HttpResult<Furniture> detail = furnitureService.detail(roomFurniture.getFurnitureid());
        Furniture furniture = detail.getData();
        //如果填写的房间配套数量大于配套剩余数量，则执行返回
        if (roomFurniture.getRomfutotla()>furniture.getFuremain()) {
            return HttpResult.fail("B0000", I18n.build(furniture.getName()+"剩余数量不足！"));
        }
        //配套使用数量等于原来使用数量加配套数量。
        furniture.setFuinuse(furniture.getFuinuse()+roomFurniture.getRomfutotla());
        furnitureService.updateFurniture(furniture);

        ParameterUtil.notNull(furniture, "数据不能为空");
        AssertUtil.isTrue(super.save(roomFurniture), "创建失败");
        return HttpResult.success("创建成功");
    }

    @Override
    public HttpResult<Serializable> updateRoomFurniture(RoomFurniture roomFurniture) {
        //查询配套表剩余配套数量
        HttpResult<Furniture> detail = furnitureService.detail(roomFurniture.getFurnitureid());
        Furniture furniture = detail.getData();
        //如果填写的房间配套数量大于配套剩余数量，则执行返回
        if (roomFurniture.getRomfutotla()>furniture.getFuremain()) {
            return HttpResult.fail("B0000", I18n.build(furniture.getName()+"剩余数量不足！"));
        }
        //调用查询当前中间表的配套数量
        HttpResult<RoomFurniture> zjbsj = myService.detail(roomFurniture.getId());
        RoomFurniture zjbsjData = zjbsj.getData();
        zjbsjData.getRomfutotla();
        if (roomFurniture.getRomfutotla()>zjbsjData.getRomfutotla()) {
            //如果填写数量大于中间表原配套数，则填写数减去原中间配套数为修改Furniture时添加
            Integer pttjs = roomFurniture.getRomfutotla()-zjbsjData.getRomfutotla();
            //配套使用数量等于原来使用数量加填写配套数量减原中间表数量
            furniture.setFuinuse(furniture.getFuinuse()+pttjs);
            furnitureService.updateFurniture(furniture);
        } if (roomFurniture.getRomfutotla()<zjbsjData.getRomfutotla()) {
            //如果填写数小于中间表原配套数，则中间表原配套数减去填写数为
            Integer pttjs = zjbsjData.getRomfutotla()-roomFurniture.getRomfutotla();
            //配套使用数量等于原来使用数量减 原中间表数量减填写配套数量
            furniture.setFuinuse(furniture.getFuinuse()-pttjs);
            furnitureService.updateFurniture(furniture);
        }

        ParameterUtil.notNull(furniture, "数据不能为空");
        AssertUtil.isTrue(super.updateById(roomFurniture), "更新失败");
        return HttpResult.success("更新成功");
    }

    @Override
    public HttpResult<RoomFurniture> detail(Long id) {
        ParameterUtil.notNull(id, "缺少数据id");
        RoomFurniture data = super.getById(id);
        ParameterUtil.notNull(data, "数据不存在", id);
        return HttpResult.data(data);
    }

    @Override
    public HttpResult<Serializable> deleteRoomFurniture(List<Long> ids) {
        ParameterUtil.notEmpty(ids, "缺少要删除的数据id");
        for (int i = 0; i < ids.size(); i++) {
            //获取中间表配套数据
            HttpResult<RoomFurniture> detail = myService.detail(ids.get(i));
            RoomFurniture roomFurniture = detail.getData();

            HttpResult<Furniture> furniture = furnitureService.detail(roomFurniture.getFurnitureid());
            Furniture data = furniture.getData();
            //配套表剩余配套数 等于 原余量加 中间表删除量
            data.setFuinuse(data.getFuinuse()-roomFurniture.getRomfutotla());
            //执行修改
            furnitureService.updateFurniture(data);
        }
        AssertUtil.isTrue(super.removeByIds(ids), "删除失败");
        return HttpResult.success("删除成功");
    }

    @Override
    public HttpResult<Serializable> updateSort(List<Map<String, BigInteger>> sortedData) {
        if (null == sortedData || 0 >= sortedData.size()) {
            return HttpResult.fail("4000", "排序数据不可为空", "error");
        }
        sortedData.forEach(item->{
            Long id = item.get("id").longValue();
            int orders = item.get("orders").intValue();
            if (!item.containsKey("id") || !item.containsKey("orders")) {
                return;
            }
            RoomFurniture roomFurniture = new RoomFurniture();
            roomFurniture.setId(id);
            roomFurniture.setOrders(orders);
            super.updateById(roomFurniture);
        });

        return HttpResult.success("排序成功");
    }


}
